<?php
	/**
	 * The loop that displays Listings. AP & SSA ACCESS ONLY.
	 *
	 */
?>

<?php /** Start the Loop **/
while ( have_posts() ) : the_post();
global $current_user;
?>
    
    <?php if ( is_user_logged_in() && ( $current_user->ID == get_post_meta($post->ID,'apID',true) || $current_user->ID == $post->post_author || is_god() ) ) : ?>
    
        <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            
            <p class="page-title"><?php the_title(); ?></p>
        
            <div class="entry-content">
                
                <?php
					if ( is_ap() ) {
						echo '<h3>Short Sale Assistant:</h3>';
							echo '<p>Name: ' . bp_core_get_userlink(get_the_author_meta('ID')) . '</p>';
					} else {
						echo '<h3>Agent / Partner Info:</h3>';
							$ap = get_user_by('id', get_post_meta($post->ID, 'apID', true));
							echo '<p>Name: ' . $ap->display_name . '</p>';
							echo '<p>Email: ' . get_post_meta( $post->ID, 'apEmail', true ) . '</p>';
							echo '<p>Phone: ' . get_post_meta( $post->ID, 'apPhone', true ) . '</p>';					
					}
					echo '<br />';

					// SSA RATING'S
						$listingStatus = get_post_meta( $post->ID, 'listingStatus', true ); // IF LISTING IS CLOSED
						if ( $listingStatus == 'Closed' ) {
							if ( is_ap() ) {
								echo '<h3>Rate your assistant:</h3>';
								rate_listing( $post->ID );
							}
							if ( is_ssa() ) {
								echo '<h3>Your performance rating:</h3>';
								rate_listing( $post->ID );
							}
							echo '<br /><br />';
						} // IF LISTING IS CLOSED
					
					echo '<h3>Listing Info:</h3>';
						echo '<p>Address: ' . get_post_meta( $post->ID, 'ListingAddress', true ) . '</p>';
						echo '<p>Price: ' . get_post_meta( $post->ID, 'ListingPrice', true ) . '</p>';
						echo '<p>Date: ' . get_post_meta( $post->ID, 'ListingDate', true ) . '</p>';
						echo '<p>Expiration Date: ' . get_post_meta( $post->ID, 'ListingExpirationDate', true ) . '</p>';
						echo '<br />';
					
					if ( $listingStatus != 'Closed' ) { // IF LISTING IS OPEN
						echo '<h3>Equator Info</h3>';
							echo '<p>Equator Username: ' . get_post_meta( $post->ID, 'EquatorUserName', true ) . '</p>';
							echo '<p>Equator Password: ' . get_post_meta( $post->ID, 'EquatorPassword', true ) . '</p>';
							echo '<br />';
						
						echo '<h3>Title Co. Info:</h3>';
							echo '<p>Name: ' . get_post_meta( $post->ID, 'titleCompany', true ) . '</p>';
							echo '<p>Contact: ' . get_post_meta( $post->ID, 'titleCoContact', true ) . '</p>';
							echo '<p>Phone: ' . get_post_meta( $post->ID, 'titleCoPhone', true ) . '</p>';
							echo '<p>Email: ' . get_post_meta( $post->ID, 'titleCoEmail', true ) . '</p>';
							echo '<br />';
						
						echo '<h3>Broker Info:</h3>';
							echo '<p>Name: ' . get_post_meta( $post->ID, 'brokerName', true ) . '</p>';
							echo '<br />';
					} // IF LISTING IS OPEN
					
					echo '<h3>Listing Status:</h3>';
						echo '<p>Status: ' . get_post_meta( $post->ID, 'listingStatus', true ) . '</p>';
						echo '<br />';
				?>
                
				<?php if ( $listingStatus != 'Closed' ) { // IF LISTING IS OPEN
					echo '<h2>Submitted Documents:</h2>';
					$args = array(
						'post_type' => 'attachment',
						'numberposts' => null,
						'post_status' => null,
						'post_parent' => $post->ID
					); 
					$attachments = get_posts($args);
					if ($attachments) {
						foreach ($attachments as $attachment) {
							echo '<p>' . get_post_meta( $attachment->ID, 'whichDoc', true ) . ' - ';
							echo '<a href="' . str_replace('/home/webadmin/trudocs.com/html', 'http://trudocs.com', get_attached_file($attachment->ID)) . '">';
							echo basename(get_attached_file($attachment->ID));
							echo '</a></p>';
						}
					} else {
						echo '<p>No documents have been submitted yet...</p>';
					}
                } // IF LISTING IS CLOSED ?>
                
            </div><!-- .entry-content -->
                    
        </div><!-- #post-## -->
        
    <?php else : ?> 
              
        <div id="post-0" class="post error404 not-found">
            <p class="page-title">Not Found</p>
            <div class="entry-content">
                <p>Apologies, but the page you requested could not be found.</p>
                <p>Try browsing the <a href="<?php echo site_url('short-sale-assistants'); ?>">Short Sale Assistant Directory</a> instead.</p>
            </div><!-- .entry-content -->
        </div><!-- #post-0 -->
        
    <?php endif; ?>

<?php endwhile; // End the loop. Whew. ?>
